package NokiaTalkOO;

use strict;
use warnings;

use Expect;
use POSIX qw(strftime);
use Net::Telnet;
use Getopt::Long;
#use Data::Validate::IP qw(is_ipv4);
use Proc::ProcessTable;
use Data::Dumper;

sub new {
	my $self = {};
	my $object = shift;
	my $ip = shift;
	my $login = shift;
	my $pwd1 = shift;
	my $pwd2 = shift;
	$self->{rExp} = undef;
	$self->{IP} = $ip;
	$self->{spawn_ok} = 0;
	$self->{connection_error} = 0;
	$self->{login} = $login;
	$self->{pwd1} = $pwd1;
	$self->{pwd2} = $pwd2;

	bless ($self);
	return $self;
}

sub setLoggingStdio
{
	my $self = shift;
	my $value = shift;

	if ( $value == 0 || $value == 1 || $value == undef ) {
		$self->{rExp}->log_stdout($value);
	}
}

sub StartTalking
{
	my $self = shift;
	my $device = shift;				#IP
	my $login = shift;
	my $telnet_pwd = shift;
	my $enable_pwd = shift;
	my $temp;

	$self->{rExp} = new Expect;

	$self->{rExp} = new Expect;
	$self->connectSSHv2();
}


sub connectSSHv2
{
	my $self = shift;

	my $spawn_ok = 0;
	my $connection_error = 0; # 1 = auth fail, 2 = login loop

	$self->{rExp}->spawn("ssh -l " . $self->{login} . " " . $self->{IP} . " 2>&1");

    my $normal_prompt = '(?i)\[[^\@^\]]*\]\#';
	my $expert_prompt = '(?i)\[Expert\@[^\]]*\]\#';

	$self->{rExp} -> expect( 15, [ timeout => sub { $spawn_ok = 0; $connection_error = 0; } ],
								 [ qr/\(yes\/no\)/,
								   sub {
									   $spawn_ok = 1;
									   $connection_error = 0;
									   my $fh = shift;
									   $fh -> send("yes\n");
									   exp_continue;
								   }
								 ],

								 [ qr/(?i)invalid input detected/,
								   sub {
									   $spawn_ok = 1;
									   $connection_error = 3;
								   }
								 ],

								 [ qr/(?i)connection timed out/,
								   sub {
									   $spawn_ok = 0;
									   $connection_error = 0;
								   }
								 ],

	                             [ qr/(?i)password:/,
								   sub {
									   $spawn_ok = 1;
									   my $fh = shift;
									   $fh -> send($self->{pwd1} . "\n");
									   exp_continue;
								   }
                                 ],
								 
								 [ qr/$normal_prompt/,
								   sub {
									   $spawn_ok = 1;
									   $connection_error = 0;
									   my $fh = shift;
									   $fh -> send("expert\n");
								   }
								 ],

								 [ qr/(?i)Permission denied/,
								   sub {
									   $spawn_ok = 1;
									   $connection_error = 1;
								   }
								 ]
						   );

	if ( $spawn_ok == 1 && $connection_error == 0 ) {
		$self->{rExp} -> expect( 15, [ timeout => sub { $spawn_ok = 1; $connection_error = 1; } ],
		                             [ qr/(?i)enter expert password:/,
									   sub {
										   $spawn_ok = 1;
										   $connection_error = 0;
										   my $fh = shift;
										   $fh -> send($self->{pwd2} . "\n");
									   }
									 ],
							   );
	}

	if ( $spawn_ok == 1 && $connection_error == 0 ) {
		$self->{rExp} -> expect( 15,
		                             [ timeout => sub { $spawn_ok = 1; $connection_error = 1; } ],
									 [ qr/$expert_prompt/,
									   sub {
									  	   $spawn_ok = 1;
										   $connection_error = 0;
									   }
									 ]
							   );
	}

	print "Spawn = " . $spawn_ok . "\n";



	$self->{spawn_ok} = $spawn_ok;
	$self->{connection_error} = $connection_error;
	if ( $spawn_ok == 0 || $connection_error != 0 ) {
		$self->{rExp}->close();
	}
	print "spawn_ok = " . $spawn_ok . "\n";
}


sub DoExecute
{
	my $self = shift;
	my $commands = shift;

	my $expert_prompt = '(?i)\[Expert\@[^\]]*\]\#'; 
	my $normal_prompt = '(?i)\[[^\@^\]]*\]\#';
	my $timeout = shift || 60;

	$self->{rExp} -> restart_timeout_upon_receive(1);

	$self->{rExp} -> send("\n");
	print "Got commands in ARRAY: \n";
	print Dumper ( $commands );
	print "\n\n";

	foreach my $command ( @{$commands} ) {
		$self->{rExp} -> expect( $timeout,
				                 [ qr/$expert_prompt/,
				                   sub {
								     my $fh = shift;
									 $fh -> send($command);
								   } 
								 ],
				               );
	}
}






sub DoExit
{
	my $self = shift;
	my $expert_prompt = '(?i)\[Expert\@[^\]]*\]\#'; 
	my $normal_prompt = '(?i)\[[^\]]*\]';

	my $spawn_ok;
	
	$self->{rExp}->send("\n");
	$self->{rExp} -> expect(   3,
			[ qr/$expert_prompt/,
				sub {
					my $fh = shift;
					$fh -> send("exit\n");
					$spawn_ok = 1;
				} 
			]);
	$self->{rExp}->send("\n");
	$self->{rExp} -> expect(   3,
			[ qr/$normal_prompt/,
				sub {
					my $fh = shift;
					$fh -> send("exit\n");
					$spawn_ok = 1;
				} 
			]);
								
   return 0;
}

sub StartInteract
{
	my $self = shift;
	
	$self->{rExp} -> interact();
}

sub GoodBye
{
	my $self = shift;
	$self->{rExp} -> soft_close();
	exit 0;
}
1;
